package com.gxan.web.renshi;

import com.alibaba.fastjson.JSONObject;
import com.gxan.pojo.SysDept;
import com.gxan.renshiservice.ServiceDept;
import com.gxan.renshiservice.impl.ServiceDeptImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/sys_dept/findDept")
public class FindDeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        ServiceDept serviceDept = new ServiceDeptImpl();
        Map map = new HashMap();
        try {
            List<SysDept> list = serviceDept.findDept();
            map.put("code",0);
            map.put("msg","success");
            map.put("data",list);
            resp.getWriter().write(JSONObject.toJSONString(map));
        } catch (SQLException e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","系统在开小差，联系管理员");
            resp.getWriter().write(JSONObject.toJSONString(map));
        }
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
